#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace KLib.Camera
{
    [RegistersService(typeof(ICameraManagerService))]
    public class CameraManager : KLib.GameComponent, ICameraManagerService
    {
        #region Fields
        private List<Camera> cameras;

        private Camera currentCamera;
        #endregion

        #region Properties
        public Camera CurrentCamera
        {
            get { return currentCamera; }
        }

        public List<Camera> Cameras
        {
            get { return cameras; }
        }


        #endregion

        #region Initialization

        public CameraManager(Game game)
            : base(game)
        {
            currentCamera = new Camera();
            Game.Services.AddService(typeof(ICamera), currentCamera);
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code here
            cameras = new List<Camera>();
            base.Initialize();
        }

        #endregion

        #region Methods
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            foreach (Camera camera in cameras)
            {
                camera.Update(gameTime);
            }
            base.Update(gameTime);
        }

        #region Adding/Removing Cameras
        public void AddCamera(Camera camera)
        {
            camera.Manager = this;
            cameras.Add(camera);
        }

        public void RemoveCamera(Camera camera)
        {
            cameras.Remove(camera);
        }

        public void RemoveCamera(int index)
        {
            cameras.RemoveAt(index);
        }

        public void RemoveAllCameras()
        {
            cameras.Clear();
            currentCamera = null;
        }
        #endregion

        #region Setting Active Cameras
        public void SetActiveCamera(Camera camera)
        {
            currentCamera = camera;
            camera.RequiresUpdate = true;

            Game.Services.RemoveService(typeof(ICamera));
            Game.Services.AddService(typeof(ICamera), currentCamera);
        }

        public void SetActiveCamera(int index)
        {
            currentCamera = cameras[index];
            currentCamera.RequiresUpdate = true;

            /*Game.Services.RemoveService(typeof(ICamera));
            Game.Services.AddService(typeof(ICamera), currentCamera);*/
        }
        #endregion

        #endregion

        #region Helper Methods

        #endregion
    }
}